﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TCP客户端连接发起测试
{
    public partial class Form1 : Form
    {
        string serverIP = "192.168.1.233";
        UInt16 serverPort = 65431;
        int linkNum = 2000;
        int interval = 100;
        int errNum = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tbServerIP.Text = serverIP;
            tbServerPort.Text = serverPort.ToString();
            tbLinkTotal.Text = linkNum.ToString();
            tbInterval.Text = interval.ToString();
        }

        private void EnableControl(bool flg)
        {
            this.Invoke(new EventHandler(delegate
            {
                tbServerIP.Enabled = flg;
                tbServerPort.Enabled = flg;
                tbLinkTotal.Enabled = flg;
                tbInterval.Enabled = flg;
                btnStart.Enabled = flg;
                tbMachineNum.Enabled = flg;
            }));
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            serverIP = tbServerIP.Text.Trim();
            serverPort = Convert.ToUInt16( tbServerPort.Text.Trim());
            linkNum = Convert.ToInt32( tbLinkTotal.Text.Trim());
            interval = Convert.ToInt32(tbInterval.Text.Trim());
            errNum = 0;
            EnableControl(false);

            if((linkNum !=0) && (interval > 30))
            {
                Thread th = new Thread(StartLinkThread);
                th.IsBackground = true;
                th.Start();
            }
        }

        private void ShowRemainLinkNum(int val)
        {
            try {
                this.Invoke(new EventHandler(delegate
                {
                    laLinkCnt.Text = val.ToString();
                }));
            } catch { }
        }

        private void StartLinkThread()
        {
            do
            {

                //定义一个套接字监听  
                Socket socketclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                //获取文本框中的IP地址  
                IPAddress address = IPAddress.Parse(serverIP);

                //将获取的IP地址和端口号绑定在网络节点上  
                IPEndPoint point = new IPEndPoint(address, serverPort);
                try {
                    //连接
                    socketclient.Connect(point);
                    //延时
                    if(cbSendHeart.Checked == true)
                    {
                        Thread.Sleep(interval-30);
                        string cmd = "#73" + Convert.ToInt32(tbMachineNum.Text.Trim()).ToString("X4") +"0012BB052^";
                        byte[] sendBuf = System.Text.Encoding.ASCII.GetBytes(cmd);
                        socketclient.Send(sendBuf);
                        Thread.Sleep(30);
                    }
                    else
                    {
                        Thread.Sleep(interval);
                    }
                    
                    //关闭连接
                    socketclient.Close();
                    socketclient = null;
                } catch {
                    errNum++;//异常+1
                }
                //计数减1
                linkNum--;
                ShowRemainLinkNum(linkNum);
            } while (linkNum > 0);

            //MessageBox.Show("已完成所有连接次数！");
            this.Invoke(new EventHandler(delegate
            {
                laLinkCnt.Text = "已完成，异常次数：" + errNum.ToString();
            }));

            EnableControl(true);
        }
    }
}
